#import "NSString+DzTool.h"
#import <CoreLocation/CoreLocation.h>
@implementation NSString (DzTool)
+ (void)saveBuyTime{
    NSDate *currentDate = [NSDate date];
    // 创建一个日历对象
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // 将当前日期加一年
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    [dateComponents setYear:1];
    NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:currentDate options:0];
    NSTimeInterval currentTimeStamp = [newDate timeIntervalSince1970];
    double timestamp = (double)(currentTimeStamp); // 如果需要毫秒精度
    [[NSUserDefaults standardUserDefaults] setDouble:timestamp forKey:@"timeDef"];
}
+ (NSTimeInterval)getBuyTime{
    return (NSTimeInterval)[[NSUserDefaults standardUserDefaults] doubleForKey:@"timeDef"];
}
+ (NSTimeInterval)getNowTime{
    NSDate *currentDate = [NSDate date];
    NSTimeInterval currentTimeStamp = [currentDate timeIntervalSince1970];
    return currentTimeStamp;
}
+ (NSString *)getCurrentTime{
    // 获取当前日期是星期几
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"]; // 日期格式
    NSString *formattedDateString = [dateFormatter stringFromDate:currentDate];
    
    int arcRandomMonthNum = arc4random_uniform(12) + 1;
    int arcRandomDayNum = arc4random_uniform(28) + 1;
    
    NSArray *formattedDates = [formattedDateString componentsSeparatedByString:@"-"];
    return [NSString stringWithFormat:@"%@-%02d-%02d",formattedDates.firstObject,arcRandomMonthNum,arcRandomDayNum];
}
+ (NSString *)makeExpiredTime:(NSTimeInterval)expiredTime{
    // 将时间戳转换为NSDate对象
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:expiredTime];
    // 创建日期格式化器
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // 设置日期格式
    // 格式化日期并输出
    NSString *formattedDate = [dateFormatter stringFromDate:date];
    return formattedDate;
}
//经纬度转具体地址
+ (void)getLocationForGeocoder:(double)latitude longitude:(double)longitude scurssBlock:(void(^)(NSString *locationName))locationNameBlock{
    // 获取用户当前位置的经纬度信息
    CLLocation *currentLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        if (error) {
            locationNameBlock(@"");
            return;
        }
        CLPlacemark *placemark = [placemarks firstObject];
        // 获取转换后的地址信息
        if (placemark.name) {
            locationNameBlock(placemark.name);
        }
    }];
}
//json转字典
+ (NSDictionary *)dicToJsonString:(NSString *)jsonString{
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
    return dictionary;
}
@end
